from django.contrib import admin
from .models import Article, ArticleImage, Like


class ArticleImageInline(admin.TabularInline):
    model = ArticleImage
    extra = 1


@admin.register(Article)
class ArticleAdmin(admin.ModelAdmin):
    list_display = ('article_id', 'title', 'user', 'nick_name', 'p_board_name', 'board_name', 'good_count', 'comment_count', 'top_type', 'post_time')
    list_filter = ('p_board', 'board', 'top_type', 'post_time')
    search_fields = ('title', 'content', 'user__nickname')
    ordering = ('-post_time',)
    readonly_fields = ('good_count', 'comment_count', 'post_time', 'update_time', 'p_board_name', 'board_name', 'nick_name')
    inlines = [ArticleImageInline]
    list_display_links = ('article_id', 'title', 'user', 'nick_name', 'p_board_name', 'board_name', 'good_count', 'comment_count', 'top_type', 'post_time')
    
    fieldsets = (
        ('Basic Information', {
            'fields': ('title', 'content', 'cover')
        }),
        ('Classified Information', {
            'fields': ('p_board', 'board', 'p_board_name', 'board_name')
        }),
        ('User information', {
            'fields': ('user', 'nick_name')
        }),
        ('Status Information', {
            'fields': ('top_type', 'good_count', 'comment_count')
        }),
        ('Time Information', {
            'fields': ('post_time', 'update_time')
        }),
    )


@admin.register(ArticleImage)
class ArticleImageAdmin(admin.ModelAdmin):
    list_display = ('image_id', 'article', 'image_path', 'create_time')
    list_filter = ('create_time',)
    search_fields = ('article__title', 'image_path')
    ordering = ('-create_time',)
    list_display_links = ('image_id', 'article', 'image_path', 'create_time')


@admin.register(Like)
class LikeAdmin(admin.ModelAdmin):
    list_display = ('like_id', 'user', 'like_type', 'object_id', 'create_time')
    list_filter = ('like_type', 'create_time')
    search_fields = ('user__nickname',)
    ordering = ('-create_time',)
    list_display_links = ('like_id', 'user', 'like_type', 'object_id', 'create_time')